home *** CD-ROM | disk | FTP | other *** search
/ Erotic Games: Memory / Erotic Games: Memory.iso / mac / air_installers / AdobeAIR.exe / setup.swf / scripts / mx / collections / Sort.as < prev    next >
Text File  |  2009-02-12  |  15KB  |  457 lines

  1. package mx.collections
  2. {
  3.    import flash.events.Event;
  4.    import flash.events.EventDispatcher;
  5.    import mx.collections.errors.SortError;
  6.    import mx.core.mx_internal;
  7.    import mx.resources.IResourceManager;
  8.    import mx.resources.ResourceManager;
  9.    import mx.utils.ObjectUtil;
  10.    
  11.    use namespace mx_internal;
  12.    
  13.    public class Sort extends EventDispatcher
  14.    {
  15.       
  16.       public static const ANY_INDEX_MODE:String = "any";
  17.       
  18.       mx_internal static const VERSION:String = "3.0.0.0";
  19.       
  20.       public static const LAST_INDEX_MODE:String = "last";
  21.       
  22.       public static const FIRST_INDEX_MODE:String = "first";
  23.        
  24.       
  25.       private var noFieldsDescending:Boolean = false;
  26.       
  27.       private var usingCustomCompareFunction:Boolean;
  28.       
  29.       private var defaultEmptyField:SortField;
  30.       
  31.       private var _fields:Array;
  32.       
  33.       private var _compareFunction:Function;
  34.       
  35.       private var _unique:Boolean;
  36.       
  37.       private var fieldList:Array;
  38.       
  39.       private var resourceManager:IResourceManager;
  40.       
  41.       public function Sort()
  42.       {
  43.          resourceManager = ResourceManager.getInstance();
  44.          fieldList = [];
  45.          super();
  46.       }
  47.       
  48.       public function get unique() : Boolean
  49.       {
  50.          return _unique;
  51.       }
  52.       
  53.       public function get compareFunction() : Function
  54.       {
  55.          return !!usingCustomCompareFunction ? _compareFunction : internalCompare;
  56.       }
  57.       
  58.       public function set unique(param1:Boolean) : void
  59.       {
  60.          _unique = param1;
  61.       }
  62.       
  63.       public function sort(param1:Array) : void
  64.       {
  65.          var fixedCompareFunction:Function = null;
  66.          var message:String = null;
  67.          var uniqueRet1:Object = null;
  68.          var fields:Array = null;
  69.          var i:int = 0;
  70.          var sortArgs:Object = null;
  71.          var uniqueRet2:Object = null;
  72.          var items:Array = param1;
  73.          if(!items || items.length <= 1)
  74.          {
  75.             return;
  76.          }
  77.          if(usingCustomCompareFunction)
  78.          {
  79.             fixedCompareFunction = function(param1:Object, param2:Object):int
  80.             {
  81.                return compareFunction(param1,param2,_fields);
  82.             };
  83.             if(unique)
  84.             {
  85.                uniqueRet1 = items.sort(fixedCompareFunction,Array.UNIQUESORT);
  86.                if(uniqueRet1 == 0)
  87.                {
  88.                   message = resourceManager.getString("collections","nonUnique");
  89.                   throw new SortError(message);
  90.                }
  91.             }
  92.             else
  93.             {
  94.                items.sort(fixedCompareFunction);
  95.             }
  96.          }
  97.          else
  98.          {
  99.             fields = this.fields;
  100.             if(fields && fields.length > 0)
  101.             {
  102.                sortArgs = initSortFields(items[0],true);
  103.                if(unique)
  104.                {
  105.                   if(sortArgs && fields.length == 1)
  106.                   {
  107.                      uniqueRet2 = items.sortOn(sortArgs.fields[0],sortArgs.options[0] | Array.UNIQUESORT);
  108.                   }
  109.                   else
  110.                   {
  111.                      uniqueRet2 = items.sort(internalCompare,Array.UNIQUESORT);
  112.                   }
  113.                   if(uniqueRet2 == 0)
  114.                   {
  115.                      message = resourceManager.getString("collections","nonUnique");
  116.                      throw new SortError(message);
  117.                   }
  118.                }
  119.                else if(sortArgs)
  120.                {
  121.                   items.sortOn(sortArgs.fields,sortArgs.options);
  122.                }
  123.                else
  124.                {
  125.                   items.sort(internalCompare);
  126.                }
  127.             }
  128.             else
  129.             {
  130.                items.sort(internalCompare);
  131.             }
  132.          }
  133.       }
  134.       
  135.       public function propertyAffectsSort(param1:String) : Boolean
  136.       {
  137.          var _loc3_:SortField = null;
  138.          if(usingCustomCompareFunction || !fields)
  139.          {
  140.             return true;
  141.          }
  142.          var _loc2_:int = 0;
  143.          while(_loc2_ < fields.length)
  144.          {
  145.             _loc3_ = fields[_loc2_];
  146.             if(_loc3_.name == param1 || _loc3_.usingCustomCompareFunction)
  147.             {
  148.                return true;
  149.             }
  150.             _loc2_++;
  151.          }
  152.          return false;
  153.       }
  154.       
  155.       private function internalCompare(param1:Object, param2:Object, param3:Array = null) : int
  156.       {
  157.          var _loc5_:int = 0;
  158.          var _loc6_:int = 0;
  159.          var _loc7_:SortField = null;
  160.          var _loc4_:int = 0;
  161.          if(!_fields)
  162.          {
  163.             _loc4_ = noFieldsCompare(param1,param2);
  164.          }
  165.          else
  166.          {
  167.             _loc5_ = 0;
  168.             _loc6_ = !!param3 ? int(param3.length) : int(_fields.length);
  169.             while(_loc4_ == 0 && _loc5_ < _loc6_)
  170.             {
  171.                _loc4_ = (_loc7_ = SortField(_fields[_loc5_])).internalCompare(param1,param2);
  172.                _loc5_++;
  173.             }
  174.          }
  175.          return _loc4_;
  176.       }
  177.       
  178.       public function reverse() : void
  179.       {
  180.          var _loc1_:int = 0;
  181.          if(fields)
  182.          {
  183.             _loc1_ = 0;
  184.             while(_loc1_ < fields.length)
  185.             {
  186.                SortField(fields[_loc1_]).reverse();
  187.                _loc1_++;
  188.             }
  189.          }
  190.          noFieldsDescending = !noFieldsDescending;
  191.       }
  192.       
  193.       private function noFieldsCompare(param1:Object, param2:Object, param3:Array = null) : int
  194.       {
  195.          var message:String = null;
  196.          var a:Object = param1;
  197.          var b:Object = param2;
  198.          var fields:Array = param3;
  199.          if(!defaultEmptyField)
  200.          {
  201.             defaultEmptyField = new SortField();
  202.             try
  203.             {
  204.                defaultEmptyField.initCompare(a);
  205.             }
  206.             catch(e:SortError)
  207.             {
  208.                message = resourceManager.getString("collections","noComparator",[a]);
  209.                throw new SortError(message);
  210.             }
  211.          }
  212.          var result:int = defaultEmptyField.compareFunction(a,b);
  213.          if(noFieldsDescending)
  214.          {
  215.             result *= -1;
  216.          }
  217.          return result;
  218.       }
  219.       
  220.       public function findItem(param1:Array, param2:Object, param3:String, param4:Boolean = false, param5:Function = null) : int
  221.       {
  222.          var compareForFind:Function = null;
  223.          var fieldsForCompare:Array = null;
  224.          var message:String = null;
  225.          var index:int = 0;
  226.          var fieldName:String = null;
  227.          var hadPreviousFieldName:Boolean = false;
  228.          var i:int = 0;
  229.          var hasFieldName:Boolean = false;
  230.          var objIndex:int = 0;
  231.          var match:Boolean = false;
  232.          var prevCompare:int = 0;
  233.          var nextCompare:int = 0;
  234.          var items:Array = param1;
  235.          var values:Object = param2;
  236.          var mode:String = param3;
  237.          var returnInsertionIndex:Boolean = param4;
  238.          var compareFunction:Function = param5;
  239.          if(!items)
  240.          {
  241.             message = resourceManager.getString("collections","noItems");
  242.             throw new SortError(message);
  243.          }
  244.          if(items.length == 0)
  245.          {
  246.             return !!returnInsertionIndex ? 1 : -1;
  247.          }
  248.          if(compareFunction == null)
  249.          {
  250.             compareForFind = this.compareFunction;
  251.             if(values && fieldList.length > 0)
  252.             {
  253.                fieldsForCompare = [];
  254.                hadPreviousFieldName = true;
  255.                i = 0;
  256.                while(true)
  257.                {
  258.                   if(i < fieldList.length)
  259.                   {
  260.                      fieldName = fieldList[i];
  261.                      if(fieldName)
  262.                      {
  263.                         try
  264.                         {
  265.                            hasFieldName = values[fieldName] !== undefined;
  266.                         }
  267.                         catch(e:Error)
  268.                         {
  269.                            hasFieldName = false;
  270.                         }
  271.                         if(hasFieldName)
  272.                         {
  273.                            if(!hadPreviousFieldName)
  274.                            {
  275.                               break;
  276.                            }
  277.                            fieldsForCompare.push(fieldName);
  278.                         }
  279.                         else
  280.                         {
  281.                            hadPreviousFieldName = false;
  282.                         }
  283.                      }
  284.                      else
  285.                      {
  286.                         fieldsForCompare.push(null);
  287.                      }
  288.                      continue;
  289.                   }
  290.                   if(fieldsForCompare.length == 0)
  291.                   {
  292.                      message = resourceManager.getString("collections","findRestriction");
  293.                      throw new SortError(message);
  294.                   }
  295.                   try
  296.                   {
  297.                      initSortFields(items[0]);
  298.                   }
  299.                   catch(initSortError:SortError)
  300.                   {
  301.                   }
  302.                }
  303.                message = resourceManager.getString("collections","findCondition",[fieldName]);
  304.                throw new SortError(message);
  305.             }
  306.          }
  307.          else
  308.          {
  309.             compareForFind = compareFunction;
  310.          }
  311.          var found:Boolean = false;
  312.          var objFound:Boolean = false;
  313.          index = 0;
  314.          var lowerBound:int = 0;
  315.          var upperBound:int = items.length - 1;
  316.          var obj:Object = null;
  317.          var direction:int = 1;
  318.          while(true)
  319.          {
  320.             if(!(!objFound && lowerBound <= upperBound))
  321.             {
  322.                if(!found && !returnInsertionIndex)
  323.                {
  324.                   return -1;
  325.                }
  326.                return direction > 0 ? int(index + 1) : int(index);
  327.             }
  328.             index = Math.round((lowerBound + upperBound) / 2);
  329.             obj = items[index];
  330.             direction = !!fieldsForCompare ? int(compareForFind(values,obj,fieldsForCompare)) : int(compareForFind(values,obj));
  331.             switch(direction)
  332.             {
  333.                case -1:
  334.                   upperBound = index - 1;
  335.                   break;
  336.                case 0:
  337.                   objFound = true;
  338.                   switch(mode)
  339.                   {
  340.                      case ANY_INDEX_MODE:
  341.                         found = true;
  342.                         break;
  343.                      case FIRST_INDEX_MODE:
  344.                         found = index == lowerBound;
  345.                         objIndex = index - 1;
  346.                         match = true;
  347.                         while(match && !found && objIndex >= lowerBound)
  348.                         {
  349.                            obj = items[objIndex];
  350.                            prevCompare = !!fieldsForCompare ? int(compareForFind(values,obj,fieldsForCompare)) : int(compareForFind(values,obj));
  351.                            match = prevCompare == 0;
  352.                            if(!match || match && objIndex == lowerBound)
  353.                            {
  354.                               found = true;
  355.                               index = objIndex + (!!match ? 0 : 1);
  356.                            }
  357.                            objIndex--;
  358.                         }
  359.                         break;
  360.                      case LAST_INDEX_MODE:
  361.                         found = index == upperBound;
  362.                         objIndex = index + 1;
  363.                         match = true;
  364.                         while(match && !found && objIndex <= upperBound)
  365.                         {
  366.                            obj = items[objIndex];
  367.                            nextCompare = !!fieldsForCompare ? int(compareForFind(values,obj,fieldsForCompare)) : int(compareForFind(values,obj));
  368.                            match = nextCompare == 0;
  369.                            if(!match || match && objIndex == upperBound)
  370.                            {
  371.                               found = true;
  372.                               index = objIndex - (!!match ? 0 : 1);
  373.                            }
  374.                            objIndex++;
  375.                         }
  376.                   }
  377.                   break;
  378.                case 1:
  379.                   lowerBound = index + 1;
  380.                   break;
  381.             }
  382.          }
  383.          message = resourceManager.getString("collections","unknownMode");
  384.          throw new SortError(message);
  385.       }
  386.       
  387.       private function initSortFields(param1:Object, param2:Boolean = false) : Object
  388.       {
  389.          var _loc4_:int = 0;
  390.          var _loc5_:SortField = null;
  391.          var _loc6_:int = 0;
  392.          var _loc3_:Object = null;
  393.          _loc4_ = 0;
  394.          while(_loc4_ < fields.length)
  395.          {
  396.             SortField(fields[_loc4_]).initCompare(param1);
  397.             _loc4_++;
  398.          }
  399.          if(param2)
  400.          {
  401.             _loc3_ = {
  402.                "fields":[],
  403.                "options":[]
  404.             };
  405.             _loc4_ = 0;
  406.             while(_loc4_ < fields.length)
  407.             {
  408.                if((_loc6_ = (_loc5_ = fields[_loc4_]).getArraySortOnOptions()) == -1)
  409.                {
  410.                   return null;
  411.                }
  412.                _loc3_.fields.push(_loc5_.name);
  413.                _loc3_.options.push(_loc6_);
  414.                _loc4_++;
  415.             }
  416.          }
  417.          return _loc3_;
  418.       }
  419.       
  420.       public function set fields(param1:Array) : void
  421.       {
  422.          var _loc2_:SortField = null;
  423.          var _loc3_:int = 0;
  424.          _fields = param1;
  425.          fieldList = [];
  426.          if(_fields)
  427.          {
  428.             _loc3_ = 0;
  429.             while(_loc3_ < _fields.length)
  430.             {
  431.                _loc2_ = SortField(_fields[_loc3_]);
  432.                fieldList.push(_loc2_.name);
  433.                _loc3_++;
  434.             }
  435.          }
  436.          dispatchEvent(new Event("fieldsChanged"));
  437.       }
  438.       
  439.       [Bindable("fieldsChanged")]
  440.       public function get fields() : Array
  441.       {
  442.          return _fields;
  443.       }
  444.       
  445.       public function set compareFunction(param1:Function) : void
  446.       {
  447.          _compareFunction = param1;
  448.          usingCustomCompareFunction = _compareFunction != null;
  449.       }
  450.       
  451.       override public function toString() : String
  452.       {
  453.          return ObjectUtil.toString(this);
  454.       }
  455.    }
  456. }
  457.